<?php
class VersionController extends ApiController
{
    public $self_error_list = array(
        'no_package'=>array('errorno' => 101111, 'errmsg'=>'您的版本是最新的'),
        );
    public function actionCheck()
    {
        $result = array();
        $platform = Yii::app()->request->getQuery('platform');
        $version = Yii::app()->request->getQuery('version');
        $type = Yii::app()->request->getQuery('type',2);//类型 1 跑男端 2.用户端
        if (true) {
            if (empty($version) || empty($platform) || !in_array($platform, array('android', 'ios'))) {
                $this->status = $this->error_list['need_param'];
                return true;
            }
            $platform_int = 0;
            switch ($platform) {
                case 'android':
                    $platform_int = Package::PLATFORM_ANDROID;
                    break;

                case 'ios':
                    $platform_int = Package::PLATFORM_IOS;
                    break;
            }
            $title = Config::model()->get('check_index_text');
            if (!empty($title)) {
                $title = '-'.$title;
            }
            $result['update_time'] = Config::model()->get('upload_addredd_time',10);//上传地理位置时间间隔秒数
            $result['check_index_text'] = ''.$title;
            $result['service_mobile'] = Config::model()->get('service_mobile','17612881143');//客服电话
            $lastPackage = Package::model()->lastPackage($platform_int,$type);
            if (!empty($lastPackage)) {
                if (version_compare($lastPackage['version_name'], $version) == 1) {
                    $result['package']['is_must'] = Config::model()->get('is_must',false);//是否 必须 升级 0  否 1 是
                    if ($result['package']['is_must'] == 1) {
                        $result['package']['is_must'] = true;
                    } else {
                        $result['package']['is_must'] = false;
                    }
                    $result['package']['changelog'] = $lastPackage['changelog'];
                    $result['package']['version'] = $lastPackage['version_name'];
                    $result['package']['allow_mini_version'] = $lastPackage['version_name'];
                    $result['package']['package_url'] = Yii::app()->request->hostInfo . '/download/' . $lastPackage['hash'];
                    if (!empty($lastPackage['url'])) {
                        $result['package']['package_url'] = $lastPackage['url'];
                    }
                    if ($platform == 'ios') {
                        //$result['package']['package_url'] = 'https://itunes.apple.com/cn/app/fu-tian-xia/id1095129585?mt=8';
                    }
                    
                }
                if (!empty($result)) {
                    $this->result = $result;
                } else {
                    $this->result = new stdClass;
                }
                // $this->keep_result_object = true;
                $this->status = $this->error_list['success'];
            } else {
                $this->status = $this->error_list['no_package'];
                    $this->result = $result;
            }
            // echo "<pre>"; print_r($this->result);exit;
        }
        
    }
}
